// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async (event, context) => {
  let thing = event.title + '-' + event.singer
  if (thing.length >= 20) {
    thing = thing.substr(0,19) + '…'
  }
  try {
    const result = await cloud.openapi.subscribeMessage.send({
      touser: event.openid,
      page: 'pages/pyq/pyq',
      lang: 'zh_CN',
      data: {
        thing2: {
          value: thing
        },
        phrase1: {
          value: event.result
        },
        thing7: {
          value: event.tip
        }
      },
      templateId: 'mgVCvgEH75iv5CyyT32bpzF7-L2qVz9Zft8Pjux5Y0Q',
      miniprogramState: 'formal'
    })
    return result
  } catch (err) {
    return err
  }
}